package com.haoxin.ai_course.modules.course.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haoxin.ai_course.common.base.AbstractController;
import com.haoxin.ai_course.common.utils.HttpJsonResult;
import com.haoxin.ai_course.modules.course.entity.AcDialogueAnswer;
import com.haoxin.ai_course.modules.course.entity.AcDialogueTopic;
import com.haoxin.ai_course.modules.course.entity.VO.AcDialogueAnswerVO;
import com.haoxin.ai_course.modules.course.entity.VO.AcDialogueTopicVO;
import com.haoxin.ai_course.modules.course.service.AcDialogueAnswerService;
import com.haoxin.ai_course.modules.course.service.AcDialogueTopicService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;



/**
 * 对话题目表
 * 
 * @author xingxiangguo
 * @email bugcn@qq.com
 * @date 2022-06-06 09:44:49
 */
@RestController
@AllArgsConstructor
@RequestMapping("/app/course/acdialoguetopic")
@Api(value="api",tags="app小程序-对话框题目管理")
public class AcDialogueTopicController  extends AbstractController {
    private final  AcDialogueTopicService acDialogueTopicService;
    private MapperFacade mapperFacade;
    
    @Autowired
    AcDialogueAnswerService acDialogueAnswerService; 
    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("对话框题目列表")
    public HttpJsonResult<List<AcDialogueTopicVO>> list(@RequestParam Map<String, Object> params){
    	HttpJsonResult<List<AcDialogueTopicVO>> result = new HttpJsonResult<List<AcDialogueTopicVO>>();
    	List<AcDialogueTopic> list = acDialogueTopicService.list();
    	List<AcDialogueTopicVO> voList = mapperFacade.mapAsList(list, AcDialogueTopicVO.class);
    	for (AcDialogueTopicVO acDialogueTopicVO : voList) {
    		QueryWrapper<AcDialogueAnswer> qw = new QueryWrapper<AcDialogueAnswer>();
    		qw.lambda().eq(AcDialogueAnswer::getTopicId, acDialogueTopicVO.getId());
        	List<AcDialogueAnswer> acDialogueAnswerList =   acDialogueAnswerService.list(qw);
        	List<AcDialogueAnswerVO>acDialogueAnswerVOList =  mapperFacade.mapAsList(acDialogueAnswerList, AcDialogueAnswerVO.class);
        	acDialogueTopicVO.setAcDialogueanswerList(acDialogueAnswerVOList);
		}
    	result.setData(voList);
        return result;
    }
    @GetMapping("info")
    @ApiOperation("根据对话框题目id获取对话框题目，包含答题项")
    public HttpJsonResult<AcDialogueTopicVO> info(@RequestParam ("id")Integer id){
    	HttpJsonResult<AcDialogueTopicVO>  result = new HttpJsonResult<AcDialogueTopicVO> ();
    	AcDialogueTopic acDialogueTopic = acDialogueTopicService.getById(id);
    	if(acDialogueTopic == null) {
    		result.setMsg("数据不存在");
    		return result;
    	}
    	AcDialogueTopicVO acDialogueTopicVO = mapperFacade.map(acDialogueTopic, AcDialogueTopicVO.class);
    	QueryWrapper<AcDialogueAnswer> qw = new QueryWrapper<AcDialogueAnswer>();
    	qw.lambda().eq(AcDialogueAnswer::getTopicId, id);
    	List<AcDialogueAnswer> acDialogueAnswerList =   acDialogueAnswerService.list(qw);
    	List<AcDialogueAnswerVO>acDialogueAnswerVOList =  mapperFacade.mapAsList(acDialogueAnswerList, AcDialogueAnswerVO.class);
    	acDialogueTopicVO.setAcDialogueanswerList(acDialogueAnswerVOList);
    	result.setData(acDialogueTopicVO);
    	return result ;
    }
    
}
